<template>
<div>
  <div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
    <el-breadcrumb-item :to="{ path: '/admin/index' }">
      <i class="el-icon-s-promotion"></i>
      管理后台首页
    </el-breadcrumb-item>
    <el-breadcrumb-item :to="{path: '/admin/ill/list'}">病例信息</el-breadcrumb-item>
    <el-breadcrumb-item>病例信息添加</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>

    <el-card class="box-card">
      <h1 style="font-size: 25px;margin-bottom: 25px;text-align: center">病例信息添加</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="老人姓名:" prop="olderId">
          <template>
            <el-select v-model="ruleForm.olderId" placeholder="请选择">
              <el-option
                  v-for="item in olderListOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="药品名称:" prop="medicineId">
          <template>
            <el-select v-model="ruleForm.medicineId" placeholder="请选择">
              <el-option
                  v-for="item in medicineListOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="疾病名称:" prop="name">
          <el-input v-model="ruleForm.name" ></el-input>
        </el-form-item>
        <el-form-item label="疾病类型:" prop="type">
          <el-input v-model="ruleForm.type"></el-input>
        </el-form-item>
        <el-form-item label="治愈方法:" prop="cure">
          <el-input v-model="ruleForm.cure"></el-input>
        </el-form-item>
        <el-form-item label="最近用药时间:" prop="medicineTime">
          <el-date-picker
              v-model="ruleForm.medicineTime"
              type="datetime"
              placeholder="选择日期" >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="病情详细:" prop="info">
          <el-input v-model="ruleForm.info" ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      olderListOptions:[],
      medicineListOptions:[],
      ruleForm: {
        olderId: '',
        olderName: '',
        medicineId: '',
        medicineName: '',
        id: '',
        name: '测试病例名称001',
        type: '痔疮',
        cure: '割了',
        medicineTime: '',
        info: '流血'
      },
      rules: {
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/ills/add-new';
          console.log('url = ' + url);
      /*    console.log('url = ' + url);
          console.log(this.ruleForm.birth);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);*/
          /*let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);*/
          /*let formData = this.qs.stringify(this.ruleForm);*/
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
              .post(url, this.ruleForm).then((response)=>{
            let responseBody = response.data;
            console.log(this.ruleForm)
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加病例信息成功！',
                type: 'success'
              });
              this.resetForm(formName);
            }else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadOlderList() {
      let url = "http://localhost:8080/olders";
      console.log("url = " + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.olderListOptions = responseBody.data;
          this.ruleForm.olderName = responseBody.date.name;
        }
      });
    },
    loadMedicineList() {
      let url = "http://localhost:8080/medicines";
      console.log("url = " + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.medicineListOptions = responseBody.data;
          this.ruleForm.medicineName = responseBody.date.name;
        }
      });
    }
  },
  mounted() {
    this.loadOlderList();
    this.loadMedicineList();
  }

}
</script>

<style>
.box-card{
  width: 80%;
  height: 800px;
  margin: 0 auto;
}
.demo-ruleForm{
  width: 50%;
  margin: 0 auto;
}

.el-input {
  width: 500px;
}
</style>